package com.atguigu.jxc.service.impl;
import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.SaleListGoodsDao;
import com.atguigu.jxc.dao.UserDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.SaleListGoodsService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.atguigu.jxc.domain.SuccessCode.SUCCESS_CODE;
import static com.atguigu.jxc.domain.SuccessCode.SUCCESS_MESS;

/**
 * @description
 */
@Service
public class SaleListGoodsServiceImpl implements SaleListGoodsService {

	@Autowired
	private LogService logService;

	@Autowired
	private UserDao userDao;

	@Autowired
	private GoodsDao goodsDao;

	@Autowired
	private SaleListGoodsDao saleListGoodsDao;

	@Transactional
	@Override
	public ServiceVO save(SaleList saleList, String saleListGoodsStr) {

		//  SaleList 持久化
		User currentUser = userDao.findUserByName((String) SecurityUtils.getSubject().getPrincipal());
		saleList.setUserId(currentUser.getUserId());
		Integer integer = saleListGoodsDao.saveSaleList(saleList);
		if (integer == null || integer == 0) {
			return new ServiceVO(200, "失败");
		}

		//  Goods 持久化
		Gson gson = new Gson();
		List<SaleListGoods> goodsList = gson.fromJson(saleListGoodsStr, new TypeToken<List<SaleListGoods>>() {
		}.getType());
		for (SaleListGoods goods : goodsList) {
			goods.setSaleListId(saleList.getSaleListId());

			//  更新库存……
			Goods byGoodsId = goodsDao.findByGoodsId(goods.getGoodsId());
			byGoodsId.setState(2);
			byGoodsId.setInventoryQuantity(byGoodsId.getInventoryQuantity() + goods.getGoodsNum());
			goodsDao.updateGoods(byGoodsId);

			Integer integer2 = saleListGoodsDao.saveSaleListGoods(goods);
			if (integer2 == null || integer2 == 0) {
				return new ServiceVO(200, "失败");
			}
		}

		return new ServiceVO(SUCCESS_CODE, SUCCESS_MESS);
	}

	@Override
	public Map<String, Object> list(String saleNumber, Integer supplierId, Integer state, String sTime, String eTime) {

		List<SaleList> list = saleListGoodsDao.selectSaleList(saleNumber, supplierId, state, sTime, eTime);
		Map<String, Object> map1 = new HashMap<>();
		map1.put("rows", list);
		return map1;
	}

	@Override
	public Map<String, Object> goodsList(Integer saleListId) {
		List<SaleListGoods> goods = saleListGoodsDao.goodsList(saleListId);
		Map<String, Object> map = new HashMap<>();
		map.put("rows",goods);
		return map;
	}

	@Override
	public ServiceVO delete(Integer saleListId) {
		saleListGoodsDao.delete(saleListId);
		return new ServiceVO(SUCCESS_CODE,SUCCESS_MESS,null);
	}
	@Override
	public ServiceVO updateState(Integer saleListId) {
		saleListGoodsDao.updateState(saleListId);

		return new ServiceVO(SUCCESS_CODE,SUCCESS_MESS,null);
	}

	@Override
	public String count(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {
		List<SaleCount> count = saleListGoodsDao.count(sTime, eTime, goodsTypeId, codeOrName);
		return new Gson().toJson(count);
	}

	@Override
	public String getSaleDataByDay(String sTime, String eTime) {
		//查销售表
		List<SaleCount> saleList = saleListGoodsDao.count(sTime, eTime, null, "");
		//查销售表  获取销售额

		//查销售明细表
		//查销售明细表        逐一  goods_id[]
		//查销售明细表        逐一  goods_id[]    逐一   goods.purchasing_price
		//                                                  计算利润

		//根据销售明细查
		List<SaleDataByDay> count = saleListGoodsDao.getSaleDataByDay(sTime, eTime);
		return new Gson().toJson(count);
	}

}
